package com.aeon.jface.treeTable.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PatternFilter;

import com.aeon.jface.tableViewer.ui.MTableModel;
import com.aeon.jface.tableViewer.ui.SymbolDescriptor;
import com.aeon.jface.tableViewer.ui.TableViewerContentProvider;

public class TreeTableViewerComposit extends Composite {

	private TreeViewer mTreeViewer;

	private Composite mTableComposite;
	private Composite mFilterComposite;

	private MTreeFilter1 mFilter;
	// private PatternFilter filter = new PatternFilter();
	private Button mButton;

	public TreeTableViewerComposit(Composite parent, int style) {
		super(parent, style);
		init();
	}

	private void init() {

		RowLayout layout = new RowLayout(SWT.VERTICAL);
		layout.fill = true;
		setLayout(layout);

		mFilter = new MTreeFilter1();

		createFilterComposite();

		createTableComposite();
		mTreeViewer.setFilters(new MTreeFilter1[] { mFilter });
		// mTreeViewer.setFilters(new PatternFilter[] { filter });

	}

	private void createFilterComposite() {
		mFilterComposite = new Composite(this, SWT.BORDER);
		RowData data = new RowData();
		data.height= 100;
		data.width = 500;
		mFilterComposite.setLayout(new FillLayout());
		final Text filterText = new Text(mFilterComposite, SWT.NONE);

		filterText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				// mFilter.setRegex(filterText.getText());
				mFilter.setPattern(filterText.getText());
				mTreeViewer.refresh();
				mTreeViewer.expandAll();
			}
		});
		mButton = new Button(mFilterComposite, SWT.PUSH);
		mButton.setText("click");
		mButton.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				ISelection iSelection = mTreeViewer.getSelection();
				// ((org.eclipse.jface.viewers.TreeSelection)iSelection).getPaths()
				System.out.println(iSelection);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void createTableComposite() {

		mTableComposite = new Composite(this, SWT.NONE);
		RowData data = new RowData();
		data.height = 500;
		data.width = 500;
		data.exclude=false;
		mTableComposite.setLayoutData(data);

		mTreeViewer = new TreeViewer(mTableComposite, SWT.FULL_SELECTION
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		mTreeViewer.getTree().setHeaderVisible(true);
		mTreeViewer.getTree().setLinesVisible(true);

		mTreeViewer.setContentProvider(new TreeTableContentProvider());

		TreeViewerColumn nameColumn = addColumns(mTreeViewer, "Parent");
		nameColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Node) {

					return ((Node) element).getmName();
				}
				return null;
			}

		});

		TreeViewerColumn descColumn = addColumns(mTreeViewer, "Children");
		descColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Node) {

					return ((Node) element).getmChildren().isEmpty() ? ((Node) element)
							.getmName() : "..";
				}
				return null;
			}

		});

		List<Node> model = prepareModel();

		TreeColumnLayout layout = new TreeColumnLayout();

		layout.setColumnData(nameColumn.getColumn(), new ColumnWeightData(10,
				20, true));
		layout.setColumnData(descColumn.getColumn(), new ColumnWeightData(10,
				20, true));

		mTableComposite.setLayout(layout);

		mTreeViewer.setInput(model);

	}

	private TreeViewerColumn addColumns(TreeViewer mTableViewer2, String header) {

		TreeViewerColumn column = new TreeViewerColumn(mTableViewer2, SWT.None);
		column.getColumn().setText(header);
		column.getColumn().setMoveable(true);
		column.getColumn().setWidth(50);
		return column;
	}

	private List<Node> prepareModel() {
		List<Node> model = new ArrayList<>();

		for (int i = 0; i < 10; i++) {
			Node p = new Node(null, "P" + i);
			for (int j = 0; j < 2; j++) {
				p.addChild(new Node(p, "c" + j));
			}
			model.add(p);
		}

		return model;
	}

}
